// 权限页面
import router from './router'
import store from './store'
router.beforeEach(async(to, from, next) => {
  if (store.state.user.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.state.user.setUserInfo.userId) {
        await store.dispatch('user/getUserInfo')
        const routes = await store.dispatch('permission/filterRoutes', store.getters.menus)
        // 把筛选出来的具有权限的动态路由数组重新添加会路由规则中
        // router.addRoutes(routes)
        // 解决跳转404问题
        router.addRoutes([...routes, { path: '*', redirect: '/404', hidden: true }])
        // 重新跳转到当前动态路由页面
        next(to.path)
      }
      next()
    }
  } else {
    const whiteArr = ['/login', '/404', '/test']
    if (whiteArr.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})
